package com.demo.supplier.controller;

import com.demo.supplier.annotation.LoginLog;
import com.demo.supplier.common.R;
import com.demo.supplier.constant.ApiConstants;
import com.demo.supplier.dto.LoginDto;
import com.demo.supplier.service.SysUserService;
import com.demo.supplier.vo.LoginVo;
import com.demo.supplier.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 认证控制器
 * 
 * @author demo
 */
@Slf4j
@RestController
@RequestMapping("${api.prefix}" + ApiConstants.AUTH)
@Api(tags = "认证管理")
public class AuthController {

    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    @LoginLog("用户登录")
    public R<LoginVo> login(@Valid @RequestBody LoginDto loginDto) {
        LoginVo loginVo = sysUserService.login(loginDto);
        return R.success("登录成功", loginVo);
    }

    @PostMapping("/logout")
    @ApiOperation("用户登出")
    public R<String> logout() {
        sysUserService.logout();
        return R.success("登出成功");
    }

    @GetMapping("/userinfo")
    @ApiOperation("获取当前用户信息")
    public R<UserVo> getUserInfo() {
        UserVo userVo = sysUserService.getCurrentUserInfo();
        return R.success(userVo);
    }
}